package com.example.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.music.mapper.LovemusicMapper;
import com.example.music.model.Lovemusic;
import com.example.music.model.Music;
import com.example.music.model.User;
import com.example.music.service.LovemusicService;
import com.example.music.utils.Constant;
import com.example.music.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * ClassName: LoveMusicController
 * Package: com.example.music.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-05-30
 * Time: 18:52
 */
@Api(tags = "喜欢音乐模块结构")
@RestController
@RequestMapping("/loveMusic")
public class LoveMusicController {

    @Resource
    private LovemusicService lovemusicService;

    @Resource
    private LovemusicMapper lovemusicMapper;

    //添加音乐到喜欢列表
    /**
     * 请求
     * post,
     * /lovemusic/likeMusic
     * data:id  //音乐的id
     */
    /**
     * 响应
     * "status":0
     * "message":"点赞音乐成功",
     * "data":true
     */
    @ApiOperation("添加喜欢的音乐")
    @PostMapping("/likeMusic")
    public Result<Boolean> likeMusic(@ApiParam("喜欢音乐的id") @RequestParam String id, HttpServletRequest request){
        //将字符串id改为整型id
        int musicId = Integer.parseInt(id);
        //通过Session获取用户ID
        HttpSession session = request.getSession();
        //判断Session是否存在
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KET) == null){
            return new Result<>(-1,"没有登录",false);
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KET);
        int userId = user.getId();
        //首先判断 该用户是否收藏了该歌曲
        QueryWrapper<Lovemusic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("music_id",musicId);
        Lovemusic lovemusic = lovemusicService.getOne(queryWrapper);
        if (lovemusic == null){
            Lovemusic music = new Lovemusic();
            music.setMusicId(musicId);
            music.setUserId(userId);
            Boolean ret = lovemusicService.save(music);
            System.out.println(ret);
            if (ret){
                return new Result<>(0,"收藏成功",true);
            }else {
                return new Result<>(-1,"收藏失败",false);
            }
        }else {
            return new Result<>(-1,"已经添加过该歌曲",false);
        }
    }

    //查询喜欢的音乐 支持模糊查询
    /**
     * 请求
     * get
     * /loveMusic/findLoveMusic
     * data:{musicName:musicName}
     */
    /**
     * 响应
     * "status":0,
     * "message":"查询到所有收藏的音乐",
     * "data":[
     *          {
     *              "id":19,
     *              "title":"烟花易冷",
     *              "singer":"周杰伦",
     *              "url":"",
     *              "time":"",
     *              "userId":
     *          },
     *          {},{},{}
     *        ]
     */
    @ApiOperation("查找喜欢的音乐")
    @GetMapping("/findLoveMusic")
    public Result<List<Music>> findLoveMusic(@ApiParam("模糊查询的值") @RequestParam(required = false) String musicName,
                                             HttpServletRequest request){
        //获取Session 并且要判断是否登录 获取到用户Id
        HttpSession session = request.getSession();
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KET) == null){
            return new Result<>(-1,"没有登录",null);
        }
        //获取当前用户的id
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KET);
        int userId = user.getId();

        List<Music> music = null;
        if (musicName != null){
            music = lovemusicMapper.findLoveMusicByKeyAnd(musicName,userId);
        }else {
            music = lovemusicMapper.findLoveMusicUserId(userId);
        }
        return new Result<>(0,"查询到了所有歌曲",music);

    }


    //移除喜欢的音乐
    /**
     * 请求
     * post,
     * /loveMusic/deleteLoveMusic
     * data:{id:id}
     */
    /**
     * 响应
     * "status":0,
     * "message":"取消收藏成功",
     * "data":true
     */
    @ApiOperation("删除喜欢的音乐")
    @PostMapping("/deleteLoveMusic")
    public Result<Boolean> deleteLoveMusic(@ApiParam("删除音乐的id") @RequestParam("id") Integer id, HttpServletRequest request){

        int musicId = id;

        //判断是否登录
        HttpSession httpSession = request.getSession();
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KET) == null){
            return new Result<>(-1,"没有登录",false);
        }
        //通过Session获取UserId取消收藏的歌曲
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KET);
        int userId = user.getId();
        QueryWrapper<Lovemusic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("music_id",musicId);
        boolean ret = lovemusicService.remove(queryWrapper);
        if (ret){
            return new Result<>(0,"移除成功",true);
        }
        return new Result<>(-1,"移除失败",false);

    }

}
